import os.path
import requests
from Apis.baseApi import BaseAPi
from Apis.loginApi import LoginAPI
from common.config import ip
from config.conf import data_path
from sql.conf import DoDb


class logAPI(BaseAPi):
    # header = {"Admin-Token":""}
    def fileLog(self,fname):
        # 上传文件
        addfileData = {"type": "img"}
        # fname = r"D:\work\files\api\datas\tp.jpg"
        fName = os.path.join(data_path,fname)
        fData = {"file": (fname, open(fName, "rb"), "image/png")}
        s1 = requests.post(f"{ip}/file/upload", data=addfileData, headers=self.header, files=fData)
        print(s1.json())
        bid = s1.json()['batchId']
        return bid
    def addlog(self,addLogData):
        # addLogData = {"categoryId": "1", "content": "今日内容~~", "tomorrow": "明日工作计划~",
        #               "question": "今日遇到的问题", "batchId": bid, "sendUserIds": "3,4"}
        s2 = requests.post(f"{ip}/OaLog/addOrUpdate", json=addLogData, headers=self.header)
        # print(s2.json())
        return s2

    # 查询
    def getLog(self):
        getLogData = {"page": 1, "limit": 5, "by": "1"}
        s = requests.post(f"{ip}/OaLog/queryList", data=getLogData, headers=self.header)
        # b_id = s.json()["data"]["list"][0]["logId"]
        return s


    #修改
    def xlog(self,xiulogData):
        cc= requests.post(f"{ip}/OaLog/addOrUpdate", json=xiulogData, headers=self.header)
        print(cc.json())
        return cc


    # 删除
    def deleteLog(self,data):
        # delLogData = {"logId": b_id}
        s4 = requests.post(f"{ip}/OaLog/deleteById", data=data, headers=self.header)
        print(s4.text)
        return s4

    def clearLogs(self):
        r = self.getLog()
        print(r.json())
        ll = r.json()["data"]["list"]
        # print(ll, type(ll))
        for i in ll:
            a = {"logId": i["logId"]}
            self.deleteLog(a)

    def getlogidsBySQL(self):
        db = DoDb()
        a = []
        d = db.dbSQL("select log_id from 72crm_oa_log WHERE create_user_id = (select user_id FROM 72crm_admin_user where username='admin' )")
        for i in d:
            a.append(i[0])
        return a




if __name__ == '__main__':
    yy = LoginAPI().login()
    token = yy.json()["Admin-Token"]
    lapi = logAPI(token)
    # lapi.header["Admin-Token"]=token
    # id = lapi.fileLog()
    # lapi.addlog(id)
    lapi.clearLogs()
